package com.na.tools.ping.deprecated;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

import com.na.tools.HttpUtils;
import com.na.tools.HttpUtils.HttpRequest;
import com.na.tools.ping.NetworkChecker;

/**
 * @see NetworkChecker
 */
@Deprecated
public class PingCheck {

//	https://cloudmonitor.ca.com/zh_cn/ping.php
//	&vtt=tt_559e1ef34ce34
//	&varghost=repo.maven.apache.org
//	&vhost=_
//	&vaction=ping
//	&ping=%E5%BC%80%E5%A7%8B
//	&vsectoken=559e1ef3587a1
	public static final String URL_PING = "https://cloudmonitor.ca.com/zh_cn/ping.php";

	public static final String PARAM_VTT = "vtt";
	public static final String PARAM_VARGHOST = "varghost"; // manual
	public static final String PARAM_VHOST = "vhost";
	public static final String PARAM_VACTION = "vaction";
	public static final String PARAM_PING = "ping"; // %E5%BC%80%E5%A7%8B 开始
	public static final String PARAM_VSECTOKEN = "vsectoken";
	
	public static final String[] DYNAMIC_PARAMS = new String[]{
		PARAM_VTT, PARAM_VHOST, PARAM_VACTION, PARAM_VSECTOKEN
	};
	
	private static final Map<String, String> CACHE = new HashMap<String, String>();
	
	public static void main(String[] args) throws Exception {
		Scanner scan = new Scanner(System.in);
		System.out.println("Please enter a host:");
		String host = scan.nextLine();
		String[] headers = new String[]{
				"Host", "cloudmonitor.ca.com",
				"Connection", "keep-alive",
				"Content-Length", "108",
				"Cache-Control", "max-age=0",
				"Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
				"Origin", "https://cloudmonitor.ca.com",
				"User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.132 Safari/537.36",
				"Content-Type", "application/x-www-form-urlencoded",
				"Referer", "https://cloudmonitor.ca.com/zh_cn/ping.php",
//				"Accept-Encoding", "gzip, deflate",
				"Accept-Language", "zh-CN,zh;q=0.8",
				"Cookie", "WMCKsession=2ef95937oukmc9l7b603mus5i2; WMCKft=32295037; _gat=1; __utmt=1; _ga=GA1.3.1996006450.1436425840; __utma=47302901.1996006450.1436425840.1436425840.1436429682.2; __utmb=47302901.8.10.1436429682; __utmc=47302901; __utmz=47302901.1436425840.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)"
		};
		String[] params = new String[]{
				PARAM_VTT, getArg(PARAM_VTT),
				PARAM_VARGHOST, host,
				PARAM_VHOST, getArg(PARAM_VHOST),
				PARAM_VACTION, getArg(PARAM_VACTION),
				PARAM_PING, "%E5%BC%80%E5%A7%8B",
				PARAM_VSECTOKEN, getArg(PARAM_VSECTOKEN)
		};
//		byte[] buf = HttpUtils.post(URL_PING,
//									PARAM_VTT, getArg(PARAM_VTT),
//									PARAM_VARGHOST, host,
//									PARAM_VHOST, getArg(PARAM_VHOST),
//									PARAM_VACTION, getArg(PARAM_VACTION),
//									PARAM_PING, "%E5%BC%80%E5%A7%8B",
//									PARAM_VSECTOKEN, getArg(PARAM_VSECTOKEN));
		HttpRequest request = new HttpRequest();
		request.url = URL_PING;
		request.method = "POST";
		request.requestHeaders = headers;
		request.requestParams = params;
		byte[] buf = HttpUtils.request(request);
		scan.close();
		System.out.println(new String(buf, "UTF-8"));
	}
	
	protected static String getArg(String key) {
		String value = CACHE.get(key);
		if (value == null) {
			tryGetInitArgs();
			value = CACHE.get(key);
		}
		return value;
	}
	
	protected static void tryGetInitArgs() {
		try {
			String html = new String(HttpUtils.get(URL_PING), "UTF-8");
			for (String p : DYNAMIC_PARAMS) {
				String v = getInputValueFromHtml(html, p);
				if (v != null) {
					CACHE.put(p, v);
				}
			}
		} catch (IOException e) {}
	}
	
	private static String getInputValueFromHtml(String html, String input) {
		int dot = html.indexOf("name=\"" + input + "\"");
		if (dot != -1) {
			dot += ("name=\"" + input + "\"").length();
			dot = html.indexOf("value=\"", dot);
			if (dot != -1) {
				dot += "value=\"".length();
				int dot2 = html.indexOf('"', dot);
				if (dot2 != -1) {
					return html.substring(dot, dot2);
				}
			}
		}
		return null;
	}
	
}
